<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属区域" prop="region">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入所属区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工位" prop="station">
        <el-input
          v-model="queryParams.station"
          placeholder="请输入工位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别1" prop="category1">
        <el-select
          v-model="queryParams.category1"
          placeholder="请选择类别1"
          clearable
          @keyup.enter.native="handleQuery"
          @change="handleSelectChange"
        >
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.l"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类别2" prop="category2">
        <el-select
          v-model="queryParams.category2"
          placeholder="请选择类别2"
          clearable
          @keyup.enter.native="handleQuery"
          @change="handleSelectChange"
        >
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.l"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="属性" prop="Stats">
        <el-input
          v-model="queryParams.Stats"
          placeholder="请输入属性"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['park2:park2:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['park2:park2:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park2:park2:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          v-hasPermi="['park2:park2:import']"
        >导入</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['park2:park2:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%;"
      :show-overflow-tooltip="true"
      empty-text="暂无数据"
    >
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="ID" align="center" prop="id"/>-->
      <el-table-column label="所属区域" align="center" prop="region"/>
      <el-table-column label="工位" align="center" prop="station"/>
      <el-table-column label="类别1" align="center" prop="category1"/>
      <el-table-column label="类别2" align="center" prop="category2"/>
      <el-table-column label="属性" align="center" prop="stats"/>
      <el-table-column label="设备名称" align="center" prop="name"/>
      <el-table-column label="设备型号" align="center" prop="model"/>
      <el-table-column label="单位" align="center" prop="unit"/>
      <el-table-column label="数量(沿用)" align="center" prop="quantity1"/>
      <el-table-column label="单价(沿用)" align="center" prop="price1"/>
      <el-table-column label="总价(沿用)" align="center" prop="Total1"/>
      <el-table-column label="数量(修改)" align="center" prop="quantity2"/>
      <el-table-column label="单价(修改)" align="center" prop="price2"/>
      <el-table-column label="总价(修改)" align="center" prop="Total2"/>
      <el-table-column label="数量(新开发)" align="center" prop="quantity3"/>
      <el-table-column label="单价(新开发)" align="center" prop="price3"/>
      <el-table-column label="总价(新开发)" align="center" prop="Total3"/>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属区域" prop="region">
              <el-input v-model="form.region" placeholder="请输入所属区域" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工位" prop="station">
              <el-input v-model="form.station" placeholder="请输入工位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别1" prop="category1">
              <el-select v-model="form.category1" placeholder="请选择类别1" >
                <el-option
                  v-for="item in options1"
                  :key="item.value"
                  :label="item.l"
                  :value="item.value"
                  @change="handleSelectChange"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别2" prop="category2">
              <el-select v-model="form.category2" placeholder="请选择类别2" >
                <el-option
                  v-for="item in options2"
                  :key="item.value"
                  :label="item.l"
                  :value="item.value"
                  @change="handleSelectChange"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="属性" prop="stats">
              <el-input v-model="form.stats" placeholder="请输入属性" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入设备型号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="数量(沿用)" prop="quantity1">
              <el-input v-model="form.quantity1" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单价(沿用)" prop="price1">
              <el-input v-model="form.price1" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总价(沿用)" prop="Total1">
              <el-input v-model="form.Total1" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="数量(修改)" prop="quantity2">
              <el-input v-model="form.quantity2" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单价(修改)" prop="price2">
              <el-input v-model="form.price2" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总价(修改)" prop="Total2">
              <el-input v-model="form.Total2" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="数量(新开发)" prop="quantity3">
              <el-input v-model="form.quantity3" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单价(新开发)" prop="price3">
              <el-input v-model="form.price3" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总价(新开发)" prop="Total3">
              <el-input v-model="form.Total3" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listPark23,getid,insertpark23,updatepark23,deletepark23} from "@/api/park2/park23";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getToken } from "@/utils/auth";

export default {
  name:"Park21",
  dicts: ['sys_normal_disable'],
  data(){
    return{
      // 遮罩层
      loading: true,
      // 唯一标识符
      uniqueId: "",
      // 选中数组
      ids: [],
      // 菜单列表
      menuOptions: [],
      // 存储用户选择的数据项
      multipleSelection: [],
      // 选中表数组
      tableNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch:true,
      // 总条数
      total: 0,
      // 表数据
      tableList: [],
      // 日期范围
      dateRange: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },

      // 用户导入参数
      upload:{
        // // 是否显示弹出层（用户导入）
        open: false,
        // // 弹出层标题（用户导入）
        title: "",
        // // 是否禁用上传
        isUploading: false,
        // // 是否更新已经存在的用户数据
        updateSupport: 0,
        // // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/park2/park23/importData"
      },

      // 查询参数
      queryParams:{
        pageNum: 1,
        pageSize: 10,
        ipaddr: undefined,
        name:undefined,
        tableName: undefined,
        tableComment: '' // 假设这是下拉列表绑定的模型
      },

      // rules: {
      //   name: [
      //     { required: true, message: "名称不能为空", trigger: "blur" }
      //   ],
      //   status: [
      //     { required: true, message: "状态不能为空", trigger: "blur" },
      //   ]
      // },
      options1: [
        // 下拉列表的选项数组
        { value: '沿用', label: '选项1' },
        { value: '改造', label: '选项2' },
        { value: '新增', label: '选项3' },
        // 可以继续添加更多选项
      ],
      options2: [
        // 下拉列表的选项数组
        { value: '自制', label: '选项1' },
        { value: '外购', label: '选项2' },
        // 可以继续添加更多选项
      ],
    }
  },
  created() {
    this.getList();
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null && time != this.uniqueId) {
      this.uniqueId = time;
      this.queryParams.pageNum = Number(this.$route.query.pageNum);
      this.getList();
    }
  },

  methods:{
    getList() {
      this.loading = true;
      listPark23(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        region: undefined,
        station: undefined,
        category1: undefined,
        category2: undefined,
        Stats: undefined,
        name: undefined,
        model: undefined,
        unit: undefined,
        quantity1: undefined,
        price1: undefined,
        Total1: undefined,
        quantity2: undefined,
        price2: undefined,
        Total2: undefined,
        quantity3: undefined,
        price3: undefined,
        Total3: undefined
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.multipleSelection =selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getid(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatepark23(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertpark23(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除ID为"' + ids + '"的数据项？').then(function() {
        return deletepark23(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message:'请选择要导出的数据',
          type:'warning',
        });
        return ;
      }
      const ids=this.multipleSelection.map(item => item.id).join(',');
      this.download('park2/park2/export', {ids}, `user_${new Date().getTime()}.xlsx`)
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('park2/park23/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    handleSelectChange(newValue) {
      // 当选择的值变化时，这里将接收新的值作为参数
      console.log('选中的值变化为:', newValue);
      // 在这里可以执行进一步的逻辑处理
    },
  }
}
</script>
